<!--
 * @Description: 历史数据
 * @Author: Ronda
 * @Date: 2022-02-16 09:30:08
 * @LastEditors: Ronda
 * @LastEditTime: 2022-05-23 15:19:45
-->
<!--
 * @Description: 历史监测数据查询
 * @Author: Ronda
 * @LastEditors: Ronda
-->
<!--
 * @Description: 历史监测数据查询
 * @Author: Ronda
 * @LastEditors: Ronda
-->
<template>
  <div>
    <el-row>
      <el-col :span="4">
        <div class="tree">
          <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input>
          <el-tree @node-click="handler" class="filter-tree" 
                   :data="empData" :props="defaultProps"
                   :filter-node-method="filterNode" ref="tree"></el-tree>
        </div>
      </el-col>
      <el-col :span="20">
        <div v-if="params.device_id">
          <el-form :inline="true" :model="params" class="query-form">
            <el-form-item label="时间范围">
              <el-date-picker
                v-model="dateRange"
                type="daterange"
                value-format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSearch">查询</el-button>
            </el-form-item>
          </el-form>

          <el-table :data="monitors" style="width: 100%" v-loading="loading">
            <el-table-column prop="insert_time" label="日期" align="center">
              <template slot-scope="scope">
                {{ scope.row.insert_time | formatDateTime }}
              </template>
            </el-table-column>
            <el-table-column prop="tsp" label="TSP" align="center"></el-table-column>
            <el-table-column prop="pm10" label="PM10" align="center"></el-table-column>
            <el-table-column prop="pm25" label="PM2.5" align="center"></el-table-column>
            <el-table-column prop="temperature" label="温度(℃)" align="center"></el-table-column>
            <el-table-column prop="humidity" label="湿度(%RH)" align="center"></el-table-column>
            <el-table-column prop="wind_direction" label="风向" width="120"></el-table-column>
            <el-table-column prop="wind_speed" label="风速(m/s)" align="center"></el-table-column>
          </el-table>

          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.page"
            :page-sizes="[10, 20, 50]"
            :page-size="params.pageSize"
            layout="total, sizes, prev, pager, next"
            :total="totalCount"
            style="margin-top: 20px;"
          ></el-pagination>
        </div>
        <div v-else style="text-align: center;color:red;line-height:50px">
          请选择设备进行查询
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { get } from '@/utils/request'
import moment from 'moment'

export default {
  filters: {
    formatDateTime(value) {
      return value ? moment(value).format('YYYY-MM-DD HH:mm:ss') : '-'
    }
  },
  data() {
    return {
      filterText: '',
      empData: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      params: {
        page: 1,
        pageSize: 100,
        engineer_id: 0,
        device_id: 0,
        begin_time: '',
        end_time: ''
      },
      dateRange: [],
      monitors: [],
      loading: false,
      totalCount: 0
    }
  },
  watch: {
    filterText(val) {
      this.$refs.tree.filter(val)
    }
  },
  methods: {
    handler(val) {
      if (!val.children) {
        this.params.engineer_id = val.engineer_id
        this.params.device_id = val.id
        this.params.page = 1;
        this.getMonitor()
      }
    },
    async handleSearch() {
      if (!this.params.device_id) {
        this.$message.warning('请先选择监测设备')
        return
      }
      if (this.dateRange && this.dateRange.length === 2) {
        this.params.begin_time = this.dateRange[0]
        this.params.end_time = this.dateRange[1]
      }
      await this.getMonitor()
    },
    async getMonitor() {
      try {
        this.loading = true
        const res = await get('/monitor/pageQuery', this.params)
        this.monitors = res.data?.list || []
        this.totalCount = res.data?.total || 0
      } finally {
        this.loading = false
      }
    },
    handleSizeChange(val) {
      this.params.pageSize = val
      this.params.page = 1
      this.getMonitor()
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.getMonitor()
    },
    filterNode(value, data) {
      if (!value) return true
      return data.name.indexOf(value) !== -1
    },
    async getEmpData() {
      const res = await get('/dashboard/findEngineerDeviceTree')
      this.empData = res.data
    }
  },
  created() {
    this.getEmpData()
  }
}
</script>

<style lang="scss" scoped>
/* 原有样式保持不变 */
.tree {
  height: calc(100vh - 60px);
  border-right: 1px solid #ededed;
  padding-right: 10px;
  margin-right: 10px;
}

.query-form {
  margin-bottom: 20px;
  .el-form-item {
    margin-right: 20px;
  }
}

.el-pagination {
  padding: 20px 0;
}
</style>